home *** CD-ROM | disk | FTP | other *** search
/ FM Towns: Free Software Collection 6 / FM Towns Free Software Collection 6.iso / t_os / w_t_ / src / wt.c < prev   
Text File  |  1993-07-08  |  5KB  |  271 lines

  1. /*
  2.      T-MENU ver2.1 level20 用
  3.          壁紙張り替えプログラム
  4.      [   W.T : ヴェー・テー  ]
  5.         (  Wachseln die Tapete )
  6.  
  7.               Version 3.0 / Level 13
  8.                   JC2653 . 02 .25
  9.                  Programmed by 紫羚
  10.  
  11. */
  12.  
  13. #include    <stdio.h>
  14. #include    <stdlib.h>
  15. #include    <string.h>
  16. #include    <sys\types.h>
  17. #include    <sys\stat.h>
  18. #include    <time.h>
  19. #include    <dos.h>
  20. #include    <ctype.h>
  21. #include    "myfunc.h"
  22.  
  23. struct List
  24.  {
  25.     char name[NSIZE] ;
  26.     struct List *pointer ;
  27.  } *root,*current;
  28.  
  29. int counter = 0 ;
  30. char tmenu[SIZE] = "" ;
  31. char Dfile[SIZE] ;
  32.  
  33. /* メイン関数 ( 前処理 ) */
  34. int main(int c,char *v[])
  35.  {
  36.     int ret = 1 ;
  37.     char lzh_file[SIZE] ;
  38.  
  39.     current = root ;
  40.     tmpnam(Dfile) ;    // テンポラリファイル名の設定
  41.     if (c == 2)
  42.      {
  43.         strcpy(lzh_file,v[1]) ;
  44.         sra2yen(lzh_file) ;    // / を\に変換する
  45.         ret = move(lzh_file) ;
  46.      }
  47.     else
  48.      {
  49.         fprintf(stderr,
  50.             "W.T. の使い方 : run386 wt lha の圧縮ファイル\n") ;
  51.      }
  52.     return (ret) ;
  53.  }
  54.  
  55. /* スラッシュを円記号に変換する */
  56. void sra2yen(char *p)
  57.  {
  58.     char *q = p ;
  59.  
  60.     while (*q != '\0')
  61.      {
  62.         if (*q == '/')
  63.          {
  64.             *q = '\\' ;
  65.          }
  66.         q++ ;
  67.      }
  68.  }
  69.  
  70. /* コピーの実行 */
  71. int move(char *lzh)
  72.  {
  73.     int ret = -1 ;
  74.     char fname[NSIZE] = "",*tp,*p ;
  75.     struct _stat buf ;
  76.  
  77.     if ((tp = getenv("TMENU")) != NULL)
  78.      {
  79.         p = strrchr(tp,'\\') ;
  80.         if ((p != NULL) && (*(p+1) == '\0'))
  81.          {
  82.             *p = '\0' ;
  83.          }
  84.         strcpy(tmenu,tp) ;
  85.         strcpy(fname,tmenu) ;
  86.      }
  87.     strcat(tmenu,"\\tmenu.tif") ;
  88.     strcat(fname,"\\") ;
  89.     select(lzh,fname) ;    // 壁紙ファイルの選択・解凍
  90.     if (_stat(fname,&buf) == 0)
  91.      {
  92.         remove(tmenu) ;        // tmenu.tif を消去する。
  93.         if (rename(fname,tmenu) != 0)
  94.          {
  95.             perror("Cannot Move a File ( to Tmenu.Tif ).") ;
  96.          }
  97.         else
  98.          {
  99.             fprintf(stderr,"\n壁紙の張り替えが終了しました。\n"
  100.                            "\t%s ==> %s\n",fname,tmenu) ;
  101.             ret = 0 ;
  102.          }
  103.      }
  104.     return(ret) ;
  105.  }
  106.  
  107. /* 壁紙ファイルの選択 */
  108. void select(char *lzh,char *dirpath)
  109.  {
  110.     char *area,*p,fname[SIZE] ;
  111.  
  112.     Make_lzhList(lzh) ;        // .lzh の中のエントリーをファイル化
  113.     if ((area = Load_file()) != NULL)    // メモリの中にエントリーをロード
  114.      {
  115.         if (Read_Entry(area) != 0)    // .tif ファイルを抽出し、リスト化
  116.          {
  117.             fprintf(stderr,".lzh ファイルの中に .tif がありません。") ;
  118.          }
  119.         else
  120.          {
  121.             p = Select_file(tmrnd() % counter) ;
  122.             strcpy(fname,dirpath) ;
  123.             strcat(fname," ") ;
  124.             strcat(fname,p) ;
  125.             strcat(dirpath,p) ;
  126.             Melt_file(lzh,fname) ;    // 壁紙ファイルを解凍
  127.          }
  128.      }
  129.  }
  130.  
  131. /* 壁紙ファイルの解凍
  132.     入力 =    lzh : 圧縮ファイルネーム
  133.             tif : 壁紙にするファイルネーム
  134. */
  135. void Melt_file(char *lzh,char *tif)
  136.  {
  137.     char Command[SIZE] = "lha x " ;
  138.  
  139.     strcat(Command,lzh) ;
  140.     strcat(Command," ") ;
  141.     strcat(Command,tif) ;
  142.  
  143.     system(Command) ;
  144.  }
  145.  
  146. /*
  147.     壁紙にするファイルをランダムに選択
  148.         ( リストの n 番目のエントリーを壁紙にする )
  149. */
  150. char *Select_file(int n)
  151.  {
  152.     int i ;
  153.     struct List *target = root->pointer ;
  154.  
  155.     for (i = 0 ; i < n ; i++)
  156.      {
  157.         target = target->pointer ;
  158.      }
  159.     return (target->name) ;
  160.  }
  161.  
  162. /* .lzh ファイルをリダイレクトでファイルに落とす */
  163. void Make_lzhList(char *lzh)
  164.  {
  165.     char Command[SIZE] = "lha l " ;
  166.  
  167.     strcat(Command,lzh) ;
  168.     strcat(Command," > ") ;        /* lha l .lzh > tempfile */
  169.     strcat(Command,Dfile) ;
  170.  
  171.     system(Command) ;
  172.  }
  173.  
  174. /* リダイレクトしたファイルをメモリーに読み込む
  175.         読み込んだ領域へのポインタを返す
  176. */
  177. char *Load_file(void)
  178.  {
  179.     char *area = NULL ;
  180.     FILE *fp ;
  181.     struct _stat Status ;
  182.  
  183.     _stat(Dfile,&Status) ;
  184.     if (Status.st_size != 0)
  185.      {
  186.         area = (char *)malloc(Status.st_size * sizeof(char) +1) ;
  187.         if (area == NULL)
  188.          {
  189.             fprintf(stderr,"メモリーが不足しています。\n") ;
  190.          }
  191.         else
  192.          {
  193.             fp = fopen(Dfile,"rb") ;
  194.             fread(area,sizeof(char),Status.st_size,fp) ;
  195.             strcat(area,"\0") ;
  196.             fclose(fp) ;
  197.          }
  198.      }
  199.     else
  200.      {
  201.         fprintf(stderr,".lzh ファイルを確認してください\n") ;
  202.      }
  203.     remove(Dfile) ;    // テンポラリファイルの抹消
  204.     return (area) ;
  205.  }
  206.  
  207. /* area ポインタの指し示す領域から、.tif のついたファイルネームを取り出す */
  208. int Read_Entry(char *area)
  209.  {
  210.     int i = 1, ret = 0 ;
  211.     char *p = area, *q, Entry[12] ;
  212.  
  213.     while ((q = strstr(p,".TIF")) != NULL)    // .TIF が領域に存在すれば
  214.      {
  215.         while (isspace(*(q-i)) == 0)    // ホワイトスペースでなければ
  216.          {
  217.             i++ ;
  218.          }
  219.         *q = '\0' ;
  220.         strcpy(Entry,q-i+1) ;
  221.         strcat(Entry,".TIF") ;
  222.         if ((ret = Make_fileList(Entry)) != 0)    // リスト化できなければ
  223.          {
  224.             break ;
  225.          }
  226.         i = 1 ;
  227.         p = q+1 ;
  228.      }
  229.     free(area) ;    // 不必要になった領域を開放
  230.     return (ret) ;
  231.  }
  232.  
  233. /* p で与えられたポインタの示す文字列をファイルネームリストに追加する */
  234. int Make_fileList(char *p)
  235.  {
  236.     int ret = 0 ;
  237.     struct List *new ;
  238.  
  239.     new = (struct List *)malloc(sizeof(struct List)) ;    // 追加領域の確保
  240.     if (new != NULL)    // 追加する領域を確保できれば
  241.      {
  242.         counter++ ;
  243.         current->pointer = new ;
  244.         current = new ;
  245.         current ->pointer = 0 ;
  246.         strcpy(current->name,p) ;
  247.      }
  248.     else
  249.      {
  250.         fprintf(stderr,"メモリーが不足しています。") ;
  251.         ret = -1 ;
  252.      }
  253.     return (ret) ;
  254.  }
  255.  
  256. /* 時間的にランダムを取る */
  257. int tmrnd(void)
  258.  {
  259.     int i,tmp,ret ;
  260.     time_t t ;
  261.  
  262.     time(&t) ;
  263.     tmp = (t % MAXINT) + (t / MAXINT) ;    // 範囲の限定 (0~MAXINT-1)
  264.     for ( i = 0 ; i < tmp ; i++)
  265.      {
  266.         rand() ;
  267.      } 
  268.     ret = rand() ;
  269.     return(ret) ;
  270.  }
  271.